package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.common.HrLogKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendanceStatusDAO;
import org.jsola.hr.dto.AttendanceStatusAddDTO;
import org.jsola.hr.dto.AttendanceStatusUpdateDTO;
import org.jsola.hr.entity.AttendanceStatusDO;
import org.jsola.hr.query.AttendanceStatusQuery;
import org.jsola.hr.service.IAttendanceStatusService;
import org.jsola.hr.vo.AttendanceStatusListVO;
import org.jsola.hr.vo.AttendanceStatusVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrAttendanceStatusServiceImpl")
public class AttendanceStatusServiceImpl implements IAttendanceStatusService {

    @Autowired
    private IAttendanceStatusDAO attendanceStatusDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendanceStatusVO save(AttendanceStatusAddDTO attendanceStatusAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendanceStatusDO attendanceStatusDO = attendanceStatusAddDTO.to(AttendanceStatusDO.class);
        // 保存
        attendanceStatusDO = save(attendanceStatusDO, tokenUser.getSiteId(), tokenUser.getUserId());
        HrLogKit.logForSave("新增员工考勤状态", "saveAttendanceStatus", attendanceStatusDO.getId(), tokenUser);
        return attendanceStatusDO.to(AttendanceStatusVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendanceStatusUpdateDTO attendanceStatusUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendanceStatusDO attendanceStatusDO = attendanceStatusUpdateDTO.to(AttendanceStatusDO.class);
        HrLogKit.logForUpdate("修改员工考勤状态", "updateAttendanceStatus", attendanceStatusDO.getId(), tokenUser);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendanceStatusDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendanceStatusIds) {
        HrLogKit.logForDelete("批量删除员工考勤状态", "deleteAttendanceStatusByIds"
                , Arrays.toString(attendanceStatusIds), tokenUser);
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendanceStatusIds);
    }


    @Override
    public AttendanceStatusVO selectById(String attendanceStatusId, String siteId) {
        AttendanceStatusDO attendanceStatusDO = selectDOById(attendanceStatusId, siteId);
        if (attendanceStatusDO == null) {
            return null;
        }
        return attendanceStatusDO.to(AttendanceStatusVO.class);
    }

    @Override
    public List<AttendanceStatusListVO> select(AttendanceStatusQuery attendanceStatusQuery, String siteId) {
        List<AttendanceStatusDO> attendanceStatusDOList = selectDO(attendanceStatusQuery, siteId);
        if(CollectionUtils.isEmpty(attendanceStatusDOList)) {
            return attendanceStatusDOList == null ? null : new ArrayList<>();
        }
        return attendanceStatusDOList.stream()
                .map(attendanceStatusDO -> attendanceStatusDO.to(AttendanceStatusListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendanceStatusQuery attendanceStatusQuery, String siteId) {
        Example example = buildExample(attendanceStatusQuery,siteId);
        return attendanceStatusDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendanceStatusListVO> selectPage(AttendanceStatusQuery attendanceStatusQuery, String siteId) {
        Example example = buildExample(attendanceStatusQuery,siteId);
        Page<AttendanceStatusDO> page = attendanceStatusDAO.selectPageByExample(example,
                attendanceStatusQuery.getPageNo(),
                attendanceStatusQuery.getPageSize());

        return page.to(AttendanceStatusListVO.class);
    }

    @Override
    public AttendanceStatusDO selectDOById(String attendanceStatusId, String siteId) {
        return listById(attendanceStatusId, siteId);
    }

    @Override
    public List<AttendanceStatusDO> selectDO(AttendanceStatusQuery attendanceStatusQuery, String siteId) {
        Example example = buildExample(attendanceStatusQuery,siteId);
        return attendanceStatusDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param attendanceStatusQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendanceStatusQuery attendanceStatusQuery, String siteId) {
        Example example = new Example(AttendanceStatusDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (attendanceStatusQuery.getRecordId() != null) {
            example.and().andLike("recordId", "%" + attendanceStatusQuery.getRecordId() + "%");
        }
        if (attendanceStatusQuery.getEmpId() != null) {
            example.and().andEqualTo("empId", attendanceStatusQuery.getEmpId());
        }
        if (attendanceStatusQuery.getNumber() != null) {
            example.and().andEqualTo("number", attendanceStatusQuery.getNumber());
        }
        if (attendanceStatusQuery.getDateStart() != null &&
                attendanceStatusQuery.getDateEnd() != null) {
            example.and().andBetween("date", attendanceStatusQuery.getDateStart(),
                    attendanceStatusQuery.getDateEnd());
        }
        if (attendanceStatusQuery.getStatus() != null) {
            example.and().andEqualTo("status", attendanceStatusQuery.getStatus());
        }
        if (attendanceStatusQuery.getMinutes() != null) {
            example.and().andEqualTo("minutes", attendanceStatusQuery.getMinutes());
        }
        if (attendanceStatusQuery.getDeduction() != null) {
            example.and().andEqualTo("deduction", attendanceStatusQuery.getDeduction());
        }
        if (attendanceStatusQuery.getRemark() != null) {
            example.and().andLike("remark", "%" + attendanceStatusQuery.getRemark() + "%");
        }
        if (attendanceStatusQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", attendanceStatusQuery.getCompanyId());
        }
        if (attendanceStatusQuery.getGmtCreateStart() != null &&
                attendanceStatusQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", attendanceStatusQuery.getGmtCreateStart(),
                    attendanceStatusQuery.getGmtCreateEnd());
        }
        if (attendanceStatusQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + attendanceStatusQuery.getCreateUserId() + "%");
        }
        if (attendanceStatusQuery.getGmtModifiedStart() != null &&
                attendanceStatusQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", attendanceStatusQuery.getGmtModifiedStart(),
                    attendanceStatusQuery.getGmtModifiedEnd());
        }
        if (attendanceStatusQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + attendanceStatusQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,attendanceStatusQuery.getOrders());
        return example;
    }
}




